using ContinuousJobs.Jobs;
using Quartz;
using Quartz.Impl.Matchers;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

builder.Services.AddQuartz(q =>
{
    q.AddJob<FirstJob>(FirstJob.Key);
    q.AddJob<SecondJob>(SecondJob.Key, j => j.StoreDurably());
    q.AddJob<ThirdJob>(ThirdJob.Key, j => j.StoreDurably());

    q.AddTrigger(t =>
    {
        t.ForJob(FirstJob.Key)
        .WithIdentity("1", "JobChainTrigger")
        .WithSimpleSchedule(sch => sch.WithIntervalInSeconds(10).RepeatForever())
        .StartNow();
    });

    q.AddJobListener<ContinuousJobListener>(GroupMatcher<JobKey>.GroupEquals("Continuous"));
});
builder.Services.AddQuartzHostedService(options =>
 {
     // when shutting down we want jobs to complete gracefully
     options.WaitForJobsToComplete = true;
 });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
